<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-25 22:10:15
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-08-01 23:02:32
 */

namespace app\models;

use Yii;

/**
 * This is the model class for table "insurance_benifit".
 *
 * @property integer $id
 * @property string $project_code
 * @property string $plan_code
 * @property string $plan_name
 * @property string $benifit_code
 * @property string $benifit_name
 * @property string $benifit_amount
 * @property string $benifit_currency
 */
class InsurancePolicy extends \yii\db\ActiveRecord
{

	const POLICY_STATUS_SUCCESS = 'ok';
	const POLICY_STATUS_PAY_SUCCESS = 'success';

    const TYPE_FFAXLX        = 'FFAXLX';
    const TYPE_FFLXWY        = 'FFLXWY';
    const TYPE_FFAYX         = 'FFAYX';
    const TYPE_FFAXLXA       = 'FFAXLXA';
    const TYPE_ABTAAP        = 'ABTAAP';

    //产品名称
    public static $productType = [
        self::TYPE_FFAXLX           => '安心留学',
        self::TYPE_FFLXWY           => '留学无忧',
        self::TYPE_FFAYX            => '爱游学',
        self::TYPE_FFAXLXA          => '趣寻安心留学全年保障计划',
        self::TYPE_ABTAAP           => '商旅通',
    ];

    //返回产品列表地址
    public static $returnUrl = [
        self::TYPE_FFAXLX           => 'http://m.funfind.cn/index/list?code=FFAXLX_FFLXWY',
        self::TYPE_FFLXWY           => 'http://m.funfind.cn/index/list?code=FFAXLX_FFLXWY',
        self::TYPE_FFAYX            => 'http://m.funfind.cn/index/list?code=FFAYX',
        self::TYPE_FFAXLXA          => 'http://m.funfind.cn/index/list?code=FFAXLX_FFLXWY',
        self::TYPE_ABTAAP           => 'http://m.funfind.cn/index/list?code=ABTAAP',
    ];

    //保单详情页
    const POLICY_DETAILS    = 'http://web.funfind.cn/details/order-details/';
    //jssdk分享接口
    const SHARE_API         = 'http://api.funfind.cn/share/share';
    //创建邀请订单接口
    const INVITE_ORDER      = 'http://api.funfind.cn/api/invite-order';
    //邀请订单支付状态
    const NOT_PAY           = 0;
    const YES_PAY           = 1;
    const PAYMENT           = 2;
    //请求接口token值
    const TOKEN             = "zxcv-asdf-qwer-1234";

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'insurance_policy';
    }

    /**
     * 根据所选的价格编号，获取相关产品信息
     */
    public static function getInsurancePolicy($user_id,$order_status=0){
        $condition  = sprintf("a.account_id=%d",$user_id);
        switch ($order_status) {
            case '1':
                $condition  = sprintf("a.account_id=%d AND c.policy_id is not null",$user_id);
                break;
            case '2':
                $condition  = sprintf("a.account_id=%d AND a.pay_time is not null",$user_id);
                break;
            case '3':
                $condition  = sprintf("a.account_id=%d AND a.issue_date is null",$user_id);
                break;
            default:
                break;
        }
        
        return static::find()->from(self::tableName() . ' as a')
            ->leftJoin(InsurancePolicyHolder::tableName() . ' as c', 'a.id = c.policy_id')
            ->where($condition)
            ->select(['a.id','a.plan_code','a.premium_total','a.project_name','c.policy_holder_name'])
            ->orderBy('a.id desc')->asArray()->limit(10)->all();
    }

    /**
     * 初始化用户操作
     */
    public static function initRecord($param){
    	$provider_id = isset($param["provider_id"]) ? $param["provider_id"] : '';
    	$plan_code = isset($param["plan_code"]) ? $param["plan_code"] : '';
        $account_id = isset($param["account_id"]) ? $param["account_id"] : '';
        $account_type = isset($param["account_type"]) ? $param["account_type"] : '';
    	
    	$userInfo = static::find()->where(['account_id'=>$account_id,'account_type' => $account_type,'provider_id'=>$provider_id,'plan_code'=>$plan_code])->limit(1)->one();

    	if (empty($userInfo)) {
    		$userInfo = new static();
            $userInfo->account_id   = $account_id;
            $userInfo->account_type = $account_type;
            $userInfo->provider_id  = $provider_id;
            $userInfo->plan_code    = $plan_code;

    		$userInfo->issue_date  = date('Y-m-d');
    		$userInfo->create_time  = date('Y-m-d H:i:s');
    	}
        if (isset($param['project_name']) && $param['project_name']) {
            $userInfo->project_name  = $param['project_name'];
        }
        if (isset($param['price_day']) && $param['price_day']) {
            $userInfo->price_day  = $param['price_day'];
        }
        if (isset($param['is_bill']) && $param['is_bill']) {
            $userInfo->is_bill  = $param['is_bill'];
        }
        if (isset($param['account_type']) && $param['account_type']) {
            $userInfo->account_type  = $param['account_type'];
        }
        if (isset($param['account_id']) && $param['account_id']) {
            $userInfo->account_id  = $param['account_id'];
        }
    	if (isset($param['effect_date']) && $param['effect_date']) {
    		$userInfo->effect_date  = $param['effect_date'];
    	}else{
            $userInfo->effect_date  = date('Y-m-d');
        }
    	if (isset($param['expire_date']) && $param['expire_date']) {
    		$userInfo->expire_date = $param['expire_date'];
    	}
    	if (isset($param['insured_amount']) && $param['insured_amount']) {
    		$userInfo->insured_amount = $param['insured_amount'];
    	}
    	if (isset($param['premium_currency']) && $param['premium_currency']) {
    		$userInfo->premium_currency = $param['premium_currency'];
    	}else{
            $userInfo->premium_currency = 'CNY';
        }
    	if (isset($param['premium_total']) && $param['premium_total']) {
    		$userInfo->premium_total = $param['premium_total'];
    	}
    	if (isset($param['pay_time']) && $param['pay_time']) {
    		$userInfo->pay_time = $param['pay_time'];
    	}
    	if (isset($param['cancel_time']) && $param['cancel_time']) {
    		$userInfo->cancel_time = $param['cancel_time'];
    	}

    	$userInfo->status = self::POLICY_STATUS_SUCCESS;
    	if ($userInfo->save()) {
    		return $userInfo->attributes['id'];
    	}
    	return false;
    }

    /**
     * 生成订单
     */
    public static function insertRecord($param){
        $provider_id = isset($param["provider_id"]) ? $param["provider_id"] : '';
        $plan_code = isset($param["plan_code"]) ? $param["plan_code"] : '';
        $account_id = isset($param["account_id"]) ? $param["account_id"] : '';
        $account_type = isset($param["account_type"]) ? $param["account_type"] : '';
        
        $userInfo = new static();
        $userInfo->account_id   = $account_id;
        $userInfo->account_type = $account_type;
        $userInfo->provider_id  = $provider_id;
        $userInfo->plan_code    = $plan_code;
        
        $userInfo->issue_date  = date('Y-m-d');
        $userInfo->create_time  = date('Y-m-d H:i:s');
        
        if (isset($param['project_name']) && $param['project_name']) {
            $userInfo->project_name  = $param['project_name'];
        }
        if (isset($param['price_day']) && $param['price_day']) {
            $userInfo->price_day  = $param['price_day'];
        }
        if (isset($param['is_bill']) && $param['is_bill']) {
            $userInfo->is_bill  = $param['is_bill'];
        }
        if (isset($param['account_type']) && $param['account_type']) {
            $userInfo->account_type  = $param['account_type'];
        }
        if (isset($param['account_id']) && $param['account_id']) {
            $userInfo->account_id  = $param['account_id'];
        }
        if (isset($param['effect_date']) && $param['effect_date']) {
            $userInfo->effect_date  = $param['effect_date'];
        }else{
            $userInfo->effect_date  = date('Y-m-d');
        }
        if (isset($param['expire_date']) && $param['expire_date']) {
            $userInfo->expire_date = $param['expire_date'];
        }
        if (isset($param['insured_amount']) && $param['insured_amount']) {
            $userInfo->insured_amount = $param['insured_amount'];
        }
        if (isset($param['premium_currency']) && $param['premium_currency']) {
            $userInfo->premium_currency = $param['premium_currency'];
        }else{
            $userInfo->premium_currency = 'CNY';
        }
        if (isset($param['premium_total']) && $param['premium_total']) {
            $userInfo->premium_total = $param['premium_total'];
        }
        if (isset($param['pay_time']) && $param['pay_time']) {
            $userInfo->pay_time = $param['pay_time'];
        }
        if (isset($param['cancel_time']) && $param['cancel_time']) {
            $userInfo->cancel_time = $param['cancel_time'];
        }
        if (isset($param['destination']) && $param['destination']) {
            $userInfo->destination = $param['destination'];
        }
        if (isset($param['soucre_tag']) && $param['soucre_tag']) {
            $userInfo->soucre_tag = $param['soucre_tag'];
        }

        $userInfo->status = self::POLICY_STATUS_SUCCESS;
        if ($userInfo->save()) {
            return $userInfo->attributes['id'];
        }
        return false;
    }
}